using System;
using System.Collections.Generic;
using System.Linq;
using Blackbird.Core.Base;
using Blackbird.Core.Interface;

namespace Blackbird.Core.Entities.Sprites
{
    [Serializable]
    public class MultiSprite : BaseMultiframe, IArtifact
    {
        public FramesetList SelectedFramesetList { get; set; }

        #region Persistent Data

        public List<FramesetList> FramesetListCollection { get; private set; }

        public string Name { get; set; }

        #endregion

        #region In-game data

        public int CurrentFrameTextureId
        {
            get { return CurrentFrame.TextureId; }
        }

        #endregion

        #region Indexers

        public FramesetList this[int index]
        {
            get { return FramesetListCollection[index]; }
        }

        public FramesetList this[string name]
        {
            get { return FramesetListCollection.SingleOrDefault(fsl => fsl.Name == name); }
        }

        public Frameset this[string framesetListName, string framesetName]
        {
            get
            {
                FramesetList list = this[Name];
                if (list != null)
                    return list[framesetName];
                return null;
            }
        }

        #endregion

        #region Constructors

        public MultiSprite()
        {
            FramesetListCollection = new List<FramesetList>();
        }

        #endregion

        #region Frame Handling

        public string SelectedFramesetListName
        {
            get { return SelectedFramesetList.Name; }
            set { SelectedFramesetList = FramesetListCollection.SingleOrDefault(fsl => fsl.Name == value); }
        }

        public string SelectedFramesetName
        {
            get { return SelectedFramesetList.SelectedFramesetName; }
            set { SetSelectedFrameset(value); }
        }

        public int SelectedFramesetIndex
        {
            get { return SelectedFramesetList.IndexOf(SelectedFramesetList.SelectedFrameset); }
            set { SetSelectedFrameset(value); }
        }

        private void SetSelectedFrameset(int index)
        {
            SelectedFramesetList.SelectedFramesetIndex = index;
            SetCurrentFrameset(SelectedFramesetList.SelectedFrameset);
        }

        private void SetSelectedFrameset(string framesetName)
        {
            SelectedFramesetList.SelectedFramesetName = framesetName;
            SetCurrentFrameset(SelectedFramesetList.SelectedFrameset);
        }

        #endregion
    }
}